<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Address as AddressModel;
use app\common\model\DeliveryArea;
use app\common\model\DeliveryArayacak;
use think\Request;

class Address extends Fornt{

	public function index(){
    $list = AddressModel::where(['user_id'=>USER_ID])->order('defaults desc')->select();
    $this->assign('list', $list);
    return $this->fetch();
	}
  public function add($id = null){
    if (IS_POST) {
				AddressModel::where('user_id', USER_ID)->update(['defaults' => 0]);
				$deliveryAreaInof = DeliveryArea::get(input('post.second_area'));
				$data = input('post.');
				$data['freight'] = $deliveryAreaInof['freight'];
				$data['defaults'] = 1;
				if(empty($data['second_area'])){
					$this->error('该地区还没开通！');
				}
				if(empty($data['freight'])){
					$data['freight'] = 0;
				}
				$this->judge_addresstype($data['first_area'], $data['type']);
				if($data['type']==1){
					//邮寄地址
					$data['address_full'] = $data['address'];
					$data['freight'] = 0; //邮寄费用
				}else{
					$data['address_full'] =  parse_config_value($deliveryAreaInof['first_area'], 'delivery_area').' '.$deliveryAreaInof['second_area'].' '.$data['address'];
				}
      	$result = model('Address')->changes($data);
      if($result){
        $this->success('添加成功！');
      }else{
        $this->error('添加失败！');
      }
    }else{
      if($id){
        $info = AddressModel::get($id);
        $this->assign('info', $info);
      }
      return $this->fetch();
    }
  }

	public function setdefaults($id = null){
		if (!$id) {
			return $this->error("非法操作！");
		}
		$info = AddressModel::find($id);
		$this->judge_addresstype($info['first_area'], $info['type']);
		$info->defaults = 1;
		AddressModel::where('user_id', USER_ID)->update(['defaults' => 0]);
		$result=	$info->save();
		if ($result) {
				return $this->success("设置成功！");
		} else {
				return $this->error("设置失败！");
		}
	}

	public function getArayacak(){
		$list = DeliveryArayacak::order('id desc')->select();
		if(cookie('?zt_address')){
			foreach ($list as $key => $value) {
				if($value->id ==  cookie('zt_address')){
					$list[$key]['default'] = 1;
				}
			}
		}
		return json($list);
	}

	public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = AddressModel::where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}

	public function map(){
		return $this->fetch();
	}

}
